package com.china08.yxyapi.api.manage.tec.teaching.lecture.pack;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePack;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePackRepository;

/**
 * 老师 ---备授课--修改授课包是否使用
 * 
 */
@CrossOrigin
@RestController
public class LectureUseUpdController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePackRepository yxyLecturePackRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/pack/use/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req,
            RespModel respModel) {
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        YxyLecturePack lecturePack = yxyLecturePackRepository.findByIdAndUserIdAndSchoolId(req.id, userId, schoolId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        Assert.notNull(lecturePack, "授课包找不到了，请刷新页面");
        lecturePack.setUse(true);
        lecturePack.setUsedCount(lecturePack.getUsedCount() + 1l);
        lecturePack.setUseDate(Integer.valueOf(simpleDateFormat.format(date)));
        yxyLecturePackRepository.save(lecturePack);
        RespModel model = new RespModel();
        model.setMessage("修改授课包使用状态成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String id;

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
